package com.example.demo.controller;

import com.example.demo.bean.UserBean;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@Controller
public class UserController {
    @Autowired
    UserService userService;
    //跳转注册界面
    //修改用户信息
    @RequestMapping(value = "/user/changeUserData",method = RequestMethod.POST)
    public Object userChangeData(HttpServletRequest request){
        Map<String,Object> userData = new HashMap<>();
        userData.put("uid",request.getParameter("uid"));
        userData.put("sex", request.getParameter("sex").trim());
        userData.put("email",request.getParameter("email").trim());
        userData.put("interest",request.getParameter("interest").trim());
        userData.put("favoriteMusic",request.getParameter("favoriteMusic").trim());
        userData.put("favoriteSinger",request.getParameter("favoriteSinger").trim());
        userData.put("readme",request.getParameter("readme").trim());
        System.out.println("用户企图修改信息："+userData.get("uid")+userData.get("sex")+","
                +userData.get("email")+","+userData.get("interest")+","+userData.get("favoriteMusic")+","
                +userData.get("favoriteSinger")+","+userData.get("readme"));
        for(Object value:userData.values()){
            if(value==null||value.toString().length()<1){
                System.out.println("注册失败，有数据为空");
                userData.put("msg","用户注册失败，有数据为空！");
                return  userData;
            }
        }
        UserBean userBean = new UserBean();
        userBean.setUid(Integer.parseInt(userData.get("uid").toString()));
        userBean.setSex(userData.get("sex").toString());
        userBean.setEmail(userData.get("email").toString());
        userBean.setInterest(userData.get("interest").toString());
        userBean.setFavorite_music(userData.get("favoriteMusic").toString());
        userBean.setFavorite_singer(userData.get("favoriteSinger").toString());
        userBean.setReadme(userData.get("readme").toString());
        if(userService.userChangeData(userBean)){
            System.out.println("修改信息成功！");
            userData.put("msg","用户修改信息成功！");
        }else {
            System.out.println("用户修改信息失败失败！");
            userData.put("msg","用户修改信息失败");
        }
        return userData;
    }

    //用户登录
    @RequestMapping(value = "/user/login",method = RequestMethod.POST)
    public Object userLogin(String account,String psw){

        UserBean userBean = userService.userLogin(account,psw);
        System.out.print("用户："+account+" : "+"密码："+psw+" ， 企图登录！");
        if(userBean==null){
            Map<String,Object> result = new HashMap<>();
            result.put("msg","用户不存在或者账号密码错误！");
            System.out.println("用户不存在或者账号密码错误！");
            return  result;
        }else {
            System.out.println("uid:"+userBean.getUid()+" ，登录成功！");
            return userBean;
        }
    }

    //用户注册
    @RequestMapping(value = "/user/regist",method = RequestMethod.POST)
    public Object userRegist(HttpServletRequest request){
        Map<String,Object> userData = new HashMap<>();
        userData.put("account",request.getParameter("account").trim());
        userData.put("psw",request.getParameter("psw").trim());
        userData.put("sex", request.getParameter("sex").trim());
        userData.put("email",request.getParameter("email").trim());
        userData.put("interest",request.getParameter("interest").trim());
        userData.put("favoriteMusic",request.getParameter("favoriteMusic").trim());
        userData.put("favoriteSinger",request.getParameter("favoriteSinger").trim());
        userData.put("readme",request.getParameter("readme").trim());
        userData.put("registionTime",new SimpleDateFormat("YYYY-MM-dd").format(new Date().getTime()));
        System.out.println("用户企图注册："+userData.get("account")+","+userData.get("psw")+","+userData.get("sex")+","
                            +userData.get("email")+","+userData.get("interest")+","+userData.get("favoriteMusic")+","
                            +userData.get("favoriteSinger")+","+userData.get("readme")+","+userData.get("registionTime"));
        for(Object value:userData.values()){
            if(value==null||value.toString().length()<1){
                System.out.println("注册失败，有数据为空");
                userData.put("msg","用户注册失败，有数据为空！");
                return  userData;
            }
        }
        UserBean userBean = new UserBean();
        userBean.setAccount(userData.get("account").toString());
        if(userService.userFingAccount(userBean.getAccount())){
            userData.put("msg","该账号已被注册！");
            return userData;
        }
        userBean.setPsw(userData.get("psw").toString());
        userBean.setSex(userData.get("sex").toString());
        userBean.setEmail(userData.get("email").toString());
        userBean.setInterest(userData.get("interest").toString());
        userBean.setFavorite_music(userData.get("favoriteMusic").toString());
        userBean.setFavorite_singer(userData.get("favoriteSinger").toString());
        userBean.setReadme(userData.get("readme").toString());
        userBean.setRegistion_time(userData.get("registionTime").toString());
        if(userService.userRegist(userBean)){

            UserBean userBean1 = userService.userLogin(userData.get("account").toString(),userData.get("psw").toString());
            if(userBean1!=null){
                userData.put("uid",userBean1.getUid());
                System.out.println("注册成功："+userData.get("uid"));
            }else {
                System.out.println("用户注册失败");
                userData.put("msg","用户注册失败");
            }
        }else {
            System.out.println("用户注册失败！");
            userData.put("msg","用户注册失败");
        }
        return userData;
    }

}
